package com.cskaoyan.market.service;


import com.cskaoyan.market.db.domain.MarketStorage;
import com.cskaoyan.market.db.domain.MarketStorageExample;
import com.cskaoyan.market.db.mapper.MarketStorageMapper;
import com.cskaoyan.market.group.Create;
import com.cskaoyan.market.util.ValidUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.beanvalidation.SpringValidatorAdapter;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.Validator;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * @author lixianbiao
 * @since 2024/06/27 20:20
 */
@Service
public class AdminStorageServiceImpl implements AdminStorageService{
    @Autowired
    private MarketStorageMapper storageMapper;
    @Value("${spring.web.resources.static-locations}")
    private String imageDirectory;
    @Value("${server.address}")
    private String address;
    @Value("${server.port}")
    private String port;

    @Autowired
    private Validator validator;

    @Override
    @Transactional
    public MarketStorage create(MultipartFile file) throws IOException{
        // 补全storage
        String name = file.getOriginalFilename();
        name = new String(name.getBytes(), StandardCharsets.UTF_8);
        String key = UUID.randomUUID()+"-"+name;
        String type = file.getContentType();
        int size = (int)file.getSize();
        String url = "http://"+address+":"+port+"/wx/storage/fetch/"+key;
        LocalDateTime now = LocalDateTime.now();
        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setKey(key);
        marketStorage.setName(name);
        marketStorage.setType(type);
        marketStorage.setSize(size);
        marketStorage.setUrl(url);
        marketStorage.setAddTime(now);
        marketStorage.setUpdateTime(now);
        marketStorage.setDeleted(false);

        // 校验参数
        ValidUtil.valid(validator,marketStorage,Create.class);

        // 插入storage
        storageMapper.insert(marketStorage);

        // 保存图像
        int index = imageDirectory.indexOf(":");
        imageDirectory = imageDirectory.substring(index+1);
        String filePath = imageDirectory + "/" + key;
        File image = new File(filePath);
        if(!image.getParentFile().exists()){
            image.getParentFile().mkdirs();
        }
        file.transferTo(image);
        return marketStorage;
    }

    @Override
    public List<MarketStorage> list(int page, int limit, String sort, String order, String key, String name) {
        MarketStorageExample marketStorageExample = new MarketStorageExample();
        marketStorageExample.setOrderByClause(sort+" "+order);
        MarketStorageExample.Criteria criteria = marketStorageExample.createCriteria();
        if(!StringUtils.isEmpty(key)){
            criteria.andKeyLike("%"+key+"%");
        }
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        PageHelper.startPage(page,limit);
        List<MarketStorage> marketStorages = storageMapper.selectByExample(marketStorageExample);
        return marketStorages;
    }
}